﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MaxLeaf.Common;
using System.Data.SqlClient;
using MaxLeaf.DB;
using System.Data;


namespace MaxLeaf.DAL.K3
{
    public class t_Base_EmpDAL : BaseDAL
    {
        /// <summary>
        ///职员
        /// </summary>
        public t_Base_EmpDAL()
            : base()
        {
            strTableName = "t_Emp";

            strFieldNames = "FAddress,FAllotPercent,FBankAccount,FBankID,FBirthday,FBrNO,FCreditAmount,FCreditDays,FCreditLevel,FCreditPeriod,FDegree,FDeleted,FDepartmentID,FDuty,FEmail,FEmpGroup,FEmpGroupID,FGender,FHireDate,FID,FIsCreditMgr,FItemDepID,FJobTypeID,FLeaveDate,FMobilePhone,FName,FNote,FNumber,FOperationGroup,FOtherAPAcctID,FOtherARAcctID,FParentID,FPhone,FPreAPAcctID,FPreARAcctID,FProfessionalGroup,FShortNumber";
        }
        protected override void Update_Before(DBAccess dbOrgCon, DBAccess dbDestCon, DataRow drOrgData, DataRow drData)
        {
            string strSql = string.Empty;
            KeyValuePair<string, IDataParameter[]> kvp = new KeyValuePair<string, IDataParameter[]>();

            
            strSql = "Update t_ICItem Set FParentID =  @FParentID  Where  FItemID=@FItemID";
            kvp = dbDestCon.CreateCondParam(new Dictionary<string, object>() { { "FParentID", drData["FParentID"] }, { "FItemID", drData["FItemID"] } });
            dbDestCon.ExecuteSql(strSql, kvp.Value);

            strSql = "Update t_Item Set FParentID =  @FParentID ,FDeleted=@FDeleted,Fnumber=@Fnumber,FName=@FName,FShortNumber=@FShortNumber,FFullNumber=@Fnumber Where  FItemID=@FItemID";
            kvp = dbDestCon.CreateCondParam(new Dictionary<string, object>() { { "FParentID", drData["FParentID"] }, { "FDeleted", drData["FDeleted"] }, { "Fnumber", drData["Fnumber"] }, { "FName", drData["FName"] }, { "FShortNumber", drData["FShortNumber"] }, { "FItemID", drData["FItemID"] } });
            dbDestCon.ExecuteSql(strSql, kvp.Value);
        }

        protected override void DAL_GetFieldData(DBAccess dbOrgCon, DBAccess dbDestCon, string strFieldName, DataRow drOrgData, DataRow drDestData)
        {
            if (strFieldName.Equals("FParentID", StringComparison.InvariantCultureIgnoreCase))
            {
                string[] arrFnumber = CvtFunc.ToString(drOrgData["Fnumber"]).Split('.');
                if (arrFnumber.Length > 1)
                {
                    drDestData[strFieldName] = GetFParentID(dbOrgCon, dbDestCon, arrFnumber);
                }
                else
                {
                    drDestData[strFieldName] = "0";
                };
            }
            else if (strFieldName.Equals("FItemID", StringComparison.InvariantCultureIgnoreCase))
            {
                string[] arrFnumber = CvtFunc.ToString(drOrgData["Fnumber"]).Split('.');
                string strFParentID = CvtFunc.ToString(drDestData["FParentID"]);
                if (strFParentID.Length == 0)
                {
                    DAL_GetFieldData(dbOrgCon, dbDestCon, "FParentID", drOrgData, drDestData);
                    strFParentID = CvtFunc.ToString(drDestData["FParentID"]);
                }
                drDestData[strFieldName] = GetFItemID(dbOrgCon, dbDestCon, CvtFunc.ToString(drOrgData["FParentID"]), strFParentID, arrFnumber);

            }
            else if (strFieldName.Equals("FDepartmentID", StringComparison.InvariantCultureIgnoreCase))
            {
                //部门
                string strFUnitGroupID = GetFDepartmentID(dbOrgCon, dbDestCon, CvtFunc.ToString(drOrgData[strFieldName]));
                if (strFUnitGroupID.Length == 0)
                {
                    drDestData[strFieldName] = DBNull.Value;
                }
                else
                {
                    drDestData[strFieldName] = strFUnitGroupID;

                   
                }

            }
            else if (strFieldName.Equals("FEmpGroup", StringComparison.InvariantCultureIgnoreCase))
            {
                //职员类别
                string strFEmpGroup = CvtFunc.ToString(drOrgData[strFieldName]);
                if (strFEmpGroup.Length == 0 || strFEmpGroup.Equals("0"))
                {
                    drDestData[strFieldName] = "0";
                }
                else
                {
                    drDestData[strFieldName] = new t_SubMessageDAL().GetDestMapKeyValue(dbOrgCon, dbDestCon, strFEmpGroup);


                }

            }

            else
            {
                base.DAL_GetFieldData(dbOrgCon, dbDestCon, strFieldName, drOrgData, drDestData);
            }
        }

        private string GetFItemID(DBAccess dbOrgCon, DBAccess dbDestCon, string strOrgFParentID, string strFParentID, string[] arrFnumber)
        {
            const string strFItemClassID = "3";
            const int intFDetail = 1;
            string strFItemId = string.Empty;
            t_ItemDAL dal = new t_ItemDAL();



            strFItemId = dal.GetFItemId(dbOrgCon, dbDestCon, strFItemClassID, arrFnumber.Length, strOrgFParentID, strFParentID, string.Join(".", arrFnumber, 0, arrFnumber.Length), intFDetail);

            UpdateAccess_t_Emp(dbDestCon, strFItemId, strFParentID);

            return strFItemId;
        }

        private string GetFParentID(DBAccess dbOrgCon, DBAccess dbDestCon, string[] arrFnumber)
        {
            const string strFItemClassID = "3";
            const int intFDetail = 0;
            string strFItemId = string.Empty;
            t_ItemDAL dal = new t_ItemDAL();
            for (int i = 0; i < arrFnumber.Length - 1; i++)
            {
                string strFParentID = "0";
                if (i != 0)
                {
                    strFParentID = strFItemId;
                }
                //得到每级的FItemId
                strFItemId = dal.GetFItemId(dbOrgCon, dbDestCon, strFItemClassID, i + 1, string.Empty, strFParentID, string.Join(".", arrFnumber, 0, i + 1), intFDetail);

                UpdateAccess_t_Emp(dbDestCon, strFItemId, strFParentID);
            }

            return strFItemId;
        }

        private void UpdateAccess_t_Emp(DBAccess dbDestCon, string strFItemId, string strFParentID)
        {
            string strSql = @"
                if not exists(select * from access_t_emp where FItemID=@FItemID)
                begin
                 Insert into access_t_emp(FItemID,FParentIDX,FDataAccessView,FDataAccessEdit,FDataAccessDelete)
                 Values(@FItemID,@FParentIDX,convert(varbinary(7200),REPLICATE(char(255),100)),convert(varbinary(7200),REPLICATE(char(255),100)),convert(varbinary(7200),REPLICATE(char(255),100)))
                end
                ";
            dbDestCon.ExecuteSql(strSql, new SqlParameter[] { new SqlParameter("@FItemID", strFItemId), new SqlParameter("@FParentIDX", strFParentID) });
        }


        private string GetFDepartmentID(DBAccess dbOrgCon, DBAccess dbDestCon, string strOrgFDepartmentID)
        {
            t_departmentDAL dal = new t_departmentDAL();


            return dal.FDepartmentID(dbOrgCon, dbDestCon, strOrgFDepartmentID);

              
        }

        protected override DataTable DAL_GetData(DBAccess dbOrgCon, DBAccess destCon, int intdataType, object objParam)
        {
            GetDataType dataType = (GetDataType)intdataType;

            if (dataType == GetDataType.OrgData)
            {
                return Gett_Base_Emp(dbOrgCon, CvtFunc.ToString(objParam));
            }


            return base.DAL_GetData(dbOrgCon, destCon, intdataType, objParam);
        }


        
        private DataTable Gett_Base_Emp(DBAccess dbOrgCon, string strFItemID)
        {
            DataTable dtt_Base_Emp = null;
            if (dtt_Base_Emp == null)
            {
                dtt_Base_Emp = dbOrgCon.GetTableData("t_Emp", new Dictionary<string, object> { { "FItemID", strFItemID } });

            }
            return dtt_Base_Emp;
        }
       
    }
}